package org.jsrml.sv.mob.controller.purchaser;

import java.util.List;
import javax.servlet.http.HttpServletRequest;

import org.jsrml.common.pagination.Pagination;
import org.jsrml.common.util.JSONResultDTO;
import org.jsrml.core.purchaser.command.RemoveWorkerCo;
import org.jsrml.core.purchaser.command.SaveWorkerCo;
import org.jsrml.core.purchaser.entity.Worker;
import org.jsrml.core.purchaser.exception.PurchaserException;
import org.jsrml.core.purchaser.qo.WorkerQO;
import org.jsrml.core.purchaser.service.WorkerService;
import org.jsrml.sv.mob.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/worker")
public class WorkerController extends BaseController {

	@Autowired
	private WorkerService workerService;

	
	@RequestMapping("/query")
	@ResponseBody
	public JSONResultDTO query(HttpServletRequest request, @RequestBody WorkerQO qo) {
		qo.setRemove(false);
		try {
			switch (qo.getResultType()) {
			case 0:
				Integer count = workerService.queryCount(qo);
				return createSuccessResult(count);
			case 1:
				Worker worker = workerService.queryUnique(qo);
				return createSuccessResult(worker);
			case 2:
				List<Worker> list = workerService.queryList(qo);
				return createSuccessResult(list);
			case 3:
				Pagination pagination = workerService.queryPagination(qo);
				return createSuccessResult(pagination);
			default:
				return createFailResult("-1", "参数中缺少正确的resultType", null);
			}
		} catch (Exception e) {
			return createFailResult("-1", e.getMessage(), null);
		}
	}
	
	
	/**
	 * 创建账号
	 */
	@RequestMapping("/create")
	@ResponseBody
	public JSONResultDTO create(HttpServletRequest request,
			@RequestBody SaveWorkerCo command) {
		Worker worker = null;
		try{
				worker = workerService.createWorker(command);
		}catch (Exception e) {
			return createFailResult("-1", e.getMessage(), null);
		}

		return createSuccessResult(worker);
	}

	/**
	 * 修改账号
	 */
	@RequestMapping("/modify")
	@ResponseBody
	public JSONResultDTO modify(HttpServletRequest request,
			@RequestBody SaveWorkerCo modifyCommand, String oldPassword) {
		Worker worker = null;
		try{
			worker = workerService.modifyWorker(modifyCommand);
		}catch (Exception e) {
			return createFailResult("-1", e.getMessage(), null);
		}
		return createSuccessResult(worker);
	}
	
	@RequestMapping(value = "/remove", method = RequestMethod.POST)
	@ResponseBody
	public JSONResultDTO remove(@RequestBody RemoveWorkerCo command) {
		
		try {
			workerService.removeWorker(command);
		} catch (PurchaserException e) {
			return createFailResult("-1", e.getMessage(), null);
		} catch (Exception e) {
			return createFailResult("-1", e.getMessage(), null);
		}
		return createSuccessResult(null);
	}
	
	@RequestMapping(value = "/pl/remove", method = RequestMethod.POST)
	@ResponseBody
	public JSONResultDTO plRemove(@RequestBody RemoveWorkerCo command) {
		try {
			workerService.plRemoveWorker(command);
		} catch (PurchaserException e) {
			return createFailResult("-1", e.getMessage(), null);
		} catch (Exception e) {
			return createFailResult("-1", e.getMessage(), null);
		}
		return createSuccessResult(null);
	}
	
	
}
